package com.fadadianqi.lottery;

import com.fadadianqi.logistics.customer.model.Customer;
import com.fadadianqi.logistics.customer.service.CustomerService;
import com.fadadianqi.goods.model.Goods;
import com.fadadianqi.logistics.sell.model.Bill;
import com.fadadianqi.logistics.sell.service.BillService;
import com.yeqiangwei.commons.util.ParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 设置抽奖规则
 * 营业额超过1000
 * 利润率超过百分之30%
 * 参与抽奖活动
 *
 * @author aroot
 */
@Controller
public class LottreryController {

    /**
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/lottery", method = RequestMethod.GET)
    public String showList(HttpServletRequest request, ModelMap modelMap) {
        String customerId = ParamUtils.getString(request, "cid", null);
        Customer customer = customerService.getById(customerId);
        String billId = ParamUtils.getString(request, "bid", null);
        Bill bill = billService.getById(billId);
        modelMap.put("customer", customer);
        modelMap.put("bill", bill);
        // 判断是否可参与抽奖
        if (lotteryService.canDoLottery(bill)) {
            List<Goods> goodsList = lotteryService.getGoods(bill);
            modelMap.put("goodsList", goodsList);
        }
        return "lottery";
    }

    @RequestMapping(value = "/lottery", method = RequestMethod.POST)
    public String doLottery(HttpServletRequest request, ModelMap modelMap) {
        String customerId = ParamUtils.getString(request, "cid", null);
        Customer customer = customerService.getById(customerId);
        String billId = ParamUtils.getString(request, "bid", null);
        Bill bill = billService.getById(billId);
        modelMap.put("customer", customer);
        modelMap.put("bill", bill);
        // 判断是否可参与抽奖
        if (lotteryService.canDoLottery(bill)) {
            List<Goods> goodsList = lotteryService.getGoods(bill);
            modelMap.put("goodsList", goodsList);
        }
        return "lottery";
    }

    @Autowired
    private LotteryService lotteryService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private BillService billService;

}
